☁️ cloud | June 28, 2021
AWS 인프라 자원을 생성할 [YAML 파일]
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameter
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
LatestAmiId:
Description: (DO NOT CHANGE)
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
AllowedValues:
- /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
ELBVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
Tags:
- Key: Name
Value: ELB-VPC
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 20.0.0.0/16
Tags:
- Key: Name
Value: My-VPC
ELBIGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: ELB-IGW
MyIGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: My-IGW
ELBIGWAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref ELBIGW
VpcId: !Ref ELBVPC
MyIGWAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref MyIGW
VpcId: !Ref MyVPC
ELBPublicRT:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref ELBVPC
Tags:
- Key: Name
Value: ELB-Public-RT
ELBDefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: ELBIGWAttachment
Properties:
RouteTableId: !Ref ELBPublicRT
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref ELBIGW
MyPublicRT:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: My-Public-RT
MyDefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: MyIGWAttachment
Properties:
RouteTableId: !Ref MyPublicRT
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref MyIGW
ELBPublicSN1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref ELBVPC
AvailabilityZone: !Select [0, !GetAZs '']
CidrBlock: 10.0.0.0/24
Tags:
- Key: Name
Value: ELB-Public-SN-1
ELBPublicSN2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref ELBVPC
AvailabilityZone: !Select [2, !GetAZs '']
CidrBlock: 10.0.1.0/24
Tags:
- Key: Name
Value: ELB-Public-SN-2
MyPublicSN:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
AvailabilityZone: !Select [0, !GetAZs '']
CidrBlock: 20.0.0.0/24
Tags:
- Key: Name
Value: My-Public-SN
ELBPublicSNRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref ELBPublicRT
SubnetId: !Ref ELBPublicSN1
ELBPublicSNRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref ELBPublicRT
SubnetId: !Ref ELBPublicSN2
MyPublicSNRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref MyPublicRT
SubnetId: !Ref MyPublicSN
MySG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP access via port 80 and SSH access via port 22
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: My-SG
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
ELBSG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP access via port 80 and SSH access via port 22
VpcId: !Ref ELBVPC
Tags:
- Key: Name
Value: ELBSG
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: udp
FromPort: '161'
ToPort: '161'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
MyEC2:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: My-EC2
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref MyPublicSN
GroupSet:
- !Ref MySG
AssociatePublicIpAddress: true
UserData:
Fn::Base64: !Sub |
#!/bin/bash
yum install net-snmp-utils -y
ELBEC21:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: ELB-EC2-1
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref ELBPublicSN1
GroupSet:
- !Ref ELBSG
AssociatePublicIpAddress: true
UserData:
Fn::Base64: !Sub |
#!/bin/bash
hostname ELB-EC2-1
yum install httpd -y
yum install net-snmp net-snmp-utils -y
yum install tcpdump -y
service httpd start
chkconfig httpd on
service snmpd start
chkconfig snmpd on
echo "<h1>ELB-EC2-1 Web Server</h1>" > /var/www/html/index.html
mkdir /var/www/html/dev
echo "<h1>ELB-EC2-1 Dev Web Page</h1>" > /var/www/html/dev/index.html
ELBEC22:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: ELB-EC2-2
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref ELBPublicSN2
GroupSet:
- !Ref ELBSG
AssociatePublicIpAddress: true
UserData:
Fn::Base64: !Sub |
#!/bin/bash
hostname ELB-EC2-2
yum install httpd -y
yum install net-snmp net-snmp-utils -y
yum install tcpdump -y
service httpd start
chkconfig httpd on
service snmpd start
chkconfig snmpd on
echo "<h1>ELB-EC2-2 Web Server</h1>" > /var/www/html/index.html
mkdir /var/www/html/mgt
echo "<h1>ELB-EC2-2 Mgt Web Page</h1>" > /var/www/html/mgt/index.html
라우팅 구성 설정
대상 그룹 대상 설정
ALB 생성 시, 프로비저닝 상태로 정상적인 동작 상태가 아님
기다리는 동안, 대상 그룹을 확인
초기 intial 상태로 상태 검사가 진행중인 상태임, 인스턴스 상태가 확인되지 않은 것을 의미
My-EC2 인스턴스에서 ALB 검증
ALB의 기본적인 로드밸런싱 알고리즘은 라운트 로빈 방식을 취하고 있음
현재 EC2-1, EC2-2 모두 index.html 페이지를 보유중인데,
dev, mgt 각각 한번은 경로를 찾고, 한번은 경로를 찾지 못함
대상 그룹 분리 및 생성
마찮가지로 Mgt도 규칙 생성
각각 총 5번씩 테스트 진행, 모두 정상적으로 트래픽이 라우팅 되는 모습
NLB 이름 설정 및 인터넷 경계 설정
리스너 라우팅 설정
NLB 대상 그룹 생성 / (위 사진에서 Create target group를 눌러 대상그룹을 NLB 생성 중간 과정에 만들었다.)
snmpget
명령어로 NLB DNS 주소로 시스템 이름(SNMP OID 1.3.6.1.2.1.1.5.0)을 요청 시, ELB-EC2-1, ELB-EC2-2가 번갈아 응답합니다.
추가로 사용자가 전달하는 요청에 대해 NLB 로드 밸런서를 거쳐갈 때, 출발지 IP를 어떻게 전달하는지 확인
3.35.55.43 > 10.0.0.57
은 출발지 IP가 3.35.x.x
라는 것으로 해당 IP는 My-EC2에 해당클라이언트 IP를 보존하지 않으면, 서버 입장에서는 어떤 대상의 IP가 접근했는지 알 수 없습니다.
이런 부분을 해결하기 위해 ALB에서는 X-Forwarded-For 헤더라는 곳에 클라이언트 IP정보를 담아 전달합니다.